package com.vcms.entity.store.item;

import java.util.HashSet;
import java.util.Set;

import com.vcms.entity.store.itemtype.StoreObject;
import com.vcms.exception.VMCSException;
import com.vcms.patterns.observer.Observable;
import com.vcms.patterns.observer.Observer;

// Copyright 1997 ISS.
// The contents contained in this document may not be reproduced in any
// form or by any means, without the written permission of ISS, other
// than for the purpose for which it has been supplied.
//
// Author: Olivo Miotto
//

public class StoreItem implements Observable {

	protected Set<Observer> observerList = new HashSet<Observer>();
	private StoreObject content;
	private int quantity;

	public StoreItem(StoreObject content, int quantity) {
		this.content = content;
		this.quantity = quantity;
	}

	public StoreObject getContent() {
		return content;
	}

	public void setContent(StoreObject ct) {
		content = ct;
	}

	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}

	public int getQuantity() {
		return quantity;
	}

	public void store() {
		quantity++;
	}

	public void decrement(int selection) throws VMCSException {
		quantity--;
		if (quantity < 0)
			quantity = 0;
		notifyObservers(selection);
	}
	
	public void increment() {
		quantity++;
	}

	@Override
	public void attach(Observer observer) {
		observerList.add(observer);
	}

	@Override
	public void detach(Observer observer) {
		observerList.remove(observer);
	}

	@Override
	public void notifyObservers(int selection)  {
		for (Observer observer : observerList) {
			try {
				observer.update(this, content, selection);
			} catch (VMCSException e) {
				e.printStackTrace();
			}
		}
	}

}
